<template>
  <div class="app-container">
    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      element-loading-text="加载中"
      :data="tableData"
      style="width: 100%"
      border
      stripe
      size="mini"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="150"> </el-table-column>
      <el-table-column prop="description" label="描述"> </el-table-column>
      <el-table-column label="中图预览">
        <template slot-scope="scope">
          <el-image
            style="width: 150px"
            :src="server_URL + scope.row.midImg"
            :preview-src-list="midImgList"
            fit="fill"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="大图预览">
        <template slot-scope="scope">
          <el-image
            style="width: 150px"
            :src="server_URL + scope.row.bigImg"
            :preview-src-list="bigImgList"
            fit="fill"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            plain
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑弹窗 -->
    <el-dialog
      title="编辑"
      :visible.sync="dialogFormVisible"
      top="5vh"
      @open="handleDialogOpen"
    >
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="首页中图">
              <Upload v-model="form.midImg" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首页大图">
              <Upload v-model="form.bigImg" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBanner, setBanner } from '@/api/banner'
import { server_URL } from '@/urlConfig'
import Upload from '@/components/Upload'
export default {
  components: {
    Upload
  },
  data() {
    return {
      tableData: [],
      server_URL,
      dialogFormVisible: false,
      form: {
        title: '',
        description: '',
        midImg: '',
        bigImg: ''
      },
      listLoading: false,
      rules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      },
      midImgList: [],
      bigImgList: []
    }
  },

  async created() {
    await this.fetchData()
  },
  methods: {
    async fetchData() {
      this.listLoading = true
      const resp = await getBanner()
      this.tableData = resp.data
      for (const i of this.tableData) {
        this.midImgList.push(server_URL + i.midImg)
        this.bigImgList.push(server_URL + i.bigImg)
      }
      this.listLoading = false
    },
    handleEdit(row) {
      this.dialogFormVisible = true
      this.form = { ...row }
    },
    async handleEditSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let params = [...this.tableData]
          for (let i = 0; i < params.length; i++) {
            if (params[i].id === this.form.id) {
              params[i] = this.form
            }
          }
          await setBanner(params)
          this.dialogFormVisible = false
          this.$message.success('编辑成功')
          await this.fetchData()
        }
      })
    },
    handleDialogOpen() {
      // 清除校验样式
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style></style>
